package com.hj.system.common.controller;

import com.hj.system.base.config.Constant;
import com.hj.system.base.exception.CustomerRuntimeException;
import com.hj.system.base.utils.FileUtil;
import com.hj.system.base.utils.ResponseUtil;
import com.hj.system.common.domain.FileDO;
import com.hj.system.common.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.Set;

/**
 * 文件处理
 *
 * @author wxf
 */

@Controller
@RequestMapping("/file")
public class FileController extends BaseController {
    @Autowired
    private FileService fileService;

    private static Set<String> readFile = new HashSet<>();

    static {
        readFile.add(".pdf");
        readFile.add(".jpg");
        readFile.add(".png");
        readFile.add(".PNG");
    }

    @GetMapping("/readAttachment/{id}")
    public void readAttachment(HttpServletResponse response, @PathVariable("id") String id) {
        FileDO fileDO = fileService.get(id);
        if (!readFile.contains(fileDO.getType())) {
            throw new CustomerRuntimeException("文件不可以预览！");
        }
        File file = new File(Constant.uploadBasePath, fileDO.getUrl());
        if (!file.exists())
            throw new CustomerRuntimeException("文件不存在！");
        try {
            FileUtil.readFile(response.getOutputStream(), new FileInputStream(file));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/getFileByKey")
    @ResponseBody
    public ResponseUtil getFileByKey(String key) {
        return ResponseUtil.okContent(fileService.getFileByKey(key));
    }

    @GetMapping("/downAttachment/{id}")
    public void export(@PathVariable("id") String id, HttpServletResponse response) throws Exception {
        FileDO fileDO = fileService.get(id);
        File file = new File(Constant.uploadBasePath, fileDO.getUrl());
        if (!file.exists()) {
            throw new CustomerRuntimeException("文件不存在！");
        }
        String fileName = fileDO.getFileName();
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        try {
            FileUtil.readFile(response.getOutputStream(), new FileInputStream(file));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @GetMapping("/viewer.html")
    public void viewer(HttpServletResponse response, String id) {
        FileDO fileDO = fileService.get(id);
        File file =null;
        if(StringUtils.isEmpty(fileDO.getOfficeUrl())){
            file = new File(Constant.uploadBasePath, fileDO.getUrl());
        }else{
            file = new File(Constant.uploadBasePath, fileDO.getOfficeUrl());
        }
        if (!file.exists())
            throw new CustomerRuntimeException("文件不存在！");
        try {
            FileUtil.readFile(response.getOutputStream(), new FileInputStream(file));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
